import SwiftUI

struct Advertisment: View {
    
    @State var number: Int = 3
    @State var shouldNavigate: Bool = false
    
    var body: some View {
        ZStack {
            Image("adv_t")
                .resizable()
                .ignoresSafeArea()
            
            VStack {
                HStack {
                    Spacer()
                    Button(action: {
                        shouldNavigate = true
                    }) {
                        HStack(spacing: 5) {
                            Image(systemName: "chevron.right.circle")
                                .font(.headline)
                            Text("跳过: \(number)")
                                .font(.headline)
                        }
                        .padding(10)
                        .foregroundColor(.secondary)
                        .background(Color.white.opacity(0.8))
                        .cornerRadius(20)
                    }
                    .frame(width: 100,height: 50)
                    .padding(.trailing, 15)
                    .padding(.top, 30)
                    .ignoresSafeArea()
                    
                }
                Spacer()
            }
        }
        .onAppear {
            startCountdown()
        }
        .fullScreenCover(isPresented: $shouldNavigate) {
            MainTabView()
        }
    }
    
    func startCountdown() {
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
            if number > 1 {
                number -= 1
            } else {
                timer.invalidate()
                shouldNavigate = true
            }
        }
    }
}

struct Advertisment_Previews: PreviewProvider {
    static var previews: some View {
        Advertisment()
    }
}
